﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace TianShu.WebApi
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class TianShuCrudController<TKey, TListDto, TCreateDto, TUpdateDto, TDetailDto> : TianShuController
    {
        //protected ICrudService<TKey, TListDto, TCreateDto, TUpdateDto, TDetailDto> CrudService;
        protected ICrudAppService<TDetailDto, TListDto, TKey, TCreateDto, TUpdateDto> CrudService;
        public TianShuCrudController(ICrudAppService<TDetailDto, TListDto, TKey, TCreateDto, TUpdateDto> crudService)
        {
            this.CrudService = crudService;
        }

        [HttpGet("getbyurl")]
        public virtual async Task<IActionResult> GetByUrl()
        {
            var urlparam = this.Request.Query.GetUrlParams();
            var result = await CrudService.GetUrlParamAsync(urlparam);
            return Json(result);
        }

        [HttpPut("insert")]
        public virtual async Task<IActionResult> Insert([FromBody] TCreateDto input)
        {
            var result = await CrudService.CreateAsync(input);
            return Json(result);
        }
        [HttpPost("update")]
        public virtual async Task<IActionResult> Update([FromQuery] TKey id, [FromBody] TUpdateDto input)
        {
            var result = await CrudService.UpdateAsync(id, input);
            return Json(result);
        }
        [HttpDelete("deleteids")]
        public virtual async Task<IActionResult> Delete([FromBody] IEnumerable<TKey> id)
        {
            var result = await CrudService.DeleteAsync(id);
            return Json(result);
        }
        [HttpDelete("delete")]
        public virtual async Task<IActionResult> Delete([FromQuery] TKey id)
        {
            var result = await CrudService.DeleteAsync(new List<TKey> { id });
            return Json(result);
        }
        [HttpGet("getbyid")]
        public virtual async Task<IActionResult> GetById(TKey id)
        {
            var result = await CrudService.GetAsync(id);
            return Json(result);
        }
    }

    public class TianShuCrudController<TKey, TListDto, TCreateDto, TUpdateDto> : TianShuCrudController<TKey, TListDto, TCreateDto, TUpdateDto, TListDto>
    {
        public TianShuCrudController(ICrudAppService<TListDto, TListDto, TKey, TCreateDto, TUpdateDto> crudService) : base(crudService)
        {
        }
    }

    public class TianShuCrudController<TKey, TListDto, TCreateUpdateDto> : TianShuCrudController<TKey, TListDto, TCreateUpdateDto, TCreateUpdateDto>
    {
        public TianShuCrudController(ICrudAppService<TListDto, TKey, TCreateUpdateDto> crudService) : base(crudService)
        {
        }
    }
    public class TianShuCrudController<TKey, TDto> : TianShuCrudController<TKey, TDto, TDto>
    {
        public TianShuCrudController(ICrudAppService<TDto, TKey> crudService) : base(crudService)
        {
        }
    }
}
